package org.zizi.action.service;

import javax.servlet.http.HttpSession;

import org.zizi.action.ActionContext;
import org.zizi.action.ActionService;
import org.zizi.action.Login;
import org.zizi.action.Logout;
import org.zizi.action.UserException;
import org.zizi.manager.LoginManager;
import org.zizi.manager.Managers;

public class LoginLogoutActionService implements ActionService
{
	public void Login(Login action) throws Exception
	{
		LoginManager im = Managers.get(LoginManager.class);
		HttpSession session = ActionContext.get().getSession();
		
		//通过识别证号，取得个人信息，并保存在manager中
		boolean ok = im.login(session.getId(), action.getUsername(), action.getPassword());
		
		if (!ok) throw new UserException("登陆失败，用户名或密码错误。请注意大小写。");
	}
	
	public void Logout(Logout action) throws Exception
	{
		LoginManager im = Managers.get(LoginManager.class);
		HttpSession session = ActionContext.get().getSession();
		
		//通过Session取得用户信息。若取得，则退出
		im.remove(session.getId());
	}
}
